package register;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Control implements ActionListener, ItemListener {
	private static JFrame jf = new JFrame("王氏集团员工综合信息查询系统");

	private static JTextArea mas = new JTextArea(200, 200);// 用于显示查询到的信息

	static JLabel condition = new JLabel("查询条件");

	static String[] word = { "查询全部", "按姓查询", "按工资排序查询", "按部门查询" };

	static JTextField jtf = new JTextField(10); // 查询条件框

	static JLabel rownumber = new JLabel("∴显示行数");

	static String[] r = { "5", "6", "7", "8", "9", "10" };

	static JComboBox choose = new JComboBox(word); // 条件选择条

	static JComboBox rownum = new JComboBox(r); // 每页显示的行数选择条

	static int page = 1; // 从第几页开始显示

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		String cho = (String) choose.getSelectedItem();// 将选择的条件选项转成String类型
		int num = Integer.parseInt((String) rownum.getSelectedItem()); // 得到每页显示的行数

		if ("查   询".equals(command)) {
			this.search(cho, num, page);
		} else if ("上一页".equals(command)) {
			if (page > 1) {
				--page;
				this.search(cho, num, page);
			}
		} else if ("下一页".equals(command)) {
			++page;
			this.search(cho, num, page);

		} else if ("退   出".equals(command)) {
			System.exit(0);
		}
	}

	public void search(String cho, int num, int page) {
		if ("查询全部".equals(cho)) {
			StringBuffer sb = Search.searchAll(num, page);
			mas.setText("员工编号\t  员工姓名\t    出生年月\t   "
					+ "   性别\t   月薪\t   部门\t    \n" + sb);
		} else if ("按姓查询".equals(cho)) {

			StringBuffer sb = Search.searchCon(cho, jtf.getText(), num, page);
			mas.setText("员工编号\t  员工姓名\t    出生年月\t   "
					+ "   性别\t   月薪\t   部门\t    \n" + sb);
		} else if ("按工资排序查询".equals(cho)) {
			StringBuffer sb = Search.searchCon(cho, null, num, page);
			mas.setText("员工编号\t  员工姓名\t    出生年月\t   "
					+ "   性别\t   月薪\t   部门\t    \n" + sb);
		} else if ("按部门查询".equals(cho)) {
			StringBuffer sb = Search.searchCon(cho, jtf.getText(), num, page);
			mas.setText("员工编号\t  员工姓名\t    出生年月\t   "
					+ "   性别\t   月薪\t   部门\t    \n" + sb);
		}
	}

	public void itemStateChanged(ItemEvent e) {

		this.jtf.setText("");
	}

	public Control() {
		mas.setEditable(false); 	// 设置信息提示框不可编辑
		mas.setColumns(10);
		mas.setRows(11);
		JPanel jp = new JPanel(); // 该容器用存放各种按钮以及条件对话框
		JPanel jpa = new JPanel(); // 用来装选择条
		JPanel jpb = new JPanel(); // 用来装按钮
		jpa.add(condition, FlowLayout.LEFT);
		jpa.add(choose);
		jpa.add(jtf);
		jpa.add(rownumber);
		jpa.add(rownum);

		String[] but = { "查   询", "上一页", "下一页", "退   出" };
		for (int i = 0; i < but.length; i++) {
			JButton jb = new JButton(but[i]);
			jb.addActionListener(this);
			jpb.add(jb);
		}

		jp.add(jpa, BorderLayout.NORTH); // 将选择条件的的容器装到二级容器。
		jp.add(jpb, BorderLayout.CENTER); // 将按钮组的容器装到二级容器

		jf.add(jp, BorderLayout.NORTH);
		jf.add(mas,BorderLayout.CENTER);

		jf.setLocation(200, 200); 					// 设置出现位置
		jf.pack(); 											// 设置窗口大小
		jf.setVisible(true);			 				// 设置可见
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String args[]) {
		new Control();
	}
}
